﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyWordAddIn.Tool
{
    class ReadFile
    {
        public string path { get; set; }

        private Dictionary<string, Dictionary<string, string>> keyValues;

        private string fileKey;

        private static ReadFile instance;

        public static ReadFile getInstance(string path)
        {

            if(null == instance)
            {
                instance = new ReadFile(path);
            }
            else
            {
                instance.path = path;
            }

            return instance;

        }


        private ReadFile(string path)
        {
            this.path = path;

            this.keyValues = new Dictionary<string, Dictionary<string, string>>();

            int start = path.LastIndexOf('\\') + 1;

            int end = path.LastIndexOf('.');

            fileKey = path.Substring(start, end - start);
        }

        public bool getDataByKey(string key, out string value)
        {
            Dictionary<string, string> map;

            bool res = this.keyValues.TryGetValue(this.fileKey, out map);

            if (res)
            {
                return map.TryGetValue(key, out value);
            }

            this.getDataByFile();

            res = this.keyValues.TryGetValue(this.fileKey, out map);

            if (res)
            {
                return map.TryGetValue(key, out value);
            }

            value = "";

            return false;
        }


        public bool readFileToData(out Dictionary<string, string> data)
        {
            Dictionary<string, string> map;

            bool res = this.keyValues.TryGetValue(this.fileKey, out map);

            if(res)
            {
                data = map;

                return res;
            }

            this.getDataByFile();

            res = this.keyValues.TryGetValue(this.fileKey, out map);

            if (res)
            {
                data = map;

                return res;
            }

            data = null;

            return false;
        }


        public void getDataByFile()
        {
            try
            {
                // 创建一个 StreamReader 的实例来读取文件 
                // using 语句也能关闭 StreamReader
                using (StreamReader sr = new StreamReader(this.path))
                {
                    string line;

                    string[] receive;

                    Dictionary<string, string> data = new Dictionary<string, string>();

                    // 从文件读取并显示行，直到文件的末尾 
                    while ((line = sr.ReadLine()) != null)
                    {
                        line = line.Replace(" ", "").Trim();

                        receive = line.Split('=');

                        data.Add(receive[0], receive[1]);
                    }

                    this.keyValues.Add(fileKey, data);
                }
            }
            catch (Exception e)
            {
                // 向用户显示出错消息
                Console.WriteLine("The file could not be read:");
                Console.WriteLine(e.Message);
            }
        }
    }
}
